/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config.javabeans;

import org.apache.log4j.Logger;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;


/**
 * Class corresponding to the "viewdef" element of views.xml file.<br>
 * This is the definition of a view.
 */

public class JBViewDef implements Serializable, Comparable
{
    /** Log4J. */
    private static Logger _logger = Logger.getLogger(JBViewDef.class);

    /** LDAP search base parameter. */
    public static final String PARAM_LDAP_BASE = "ldap-base";

    /** View name. */
    private String name;

    /** Dynamic view flag. */
    private String dynamic;

    /** Plugin class name. */
    private String plugin;

    /** URL associated with nodes expansion. */
    private String expandUrl;

    /** Label URL. */
    private String label;

    /** Associated icon. */
    private String icon;

    /** URL associated with top level node. */
    private String labelURL;

    /** View specific parameters. */
    private Properties param = new Properties();

    /** Objectviews included in this view (JBObjectView). */
    private Map objectviewMap = new HashMap();

    /** List of level definitions (JBLevel). */
    private List levelList = new ArrayList();

    /** List of profiles wich can see this view. */
    private JBViewDefProfiles jbViewDefProfiles;

    /** Creates new JBViewDef. */
    public JBViewDef()
    {
    }

    /**
     * Gets LDAP search base if specified for this view.
     *
     * @return search base or null if not specified.
     */
    public String getLdapBaseParam()
    {
        String baseSearch = param.getProperty(PARAM_LDAP_BASE);

        return baseSearch;
    }

    /**
     * Checks whether the view is dynamic.
     *
     * @return "true" if it is, else "false".
     */
    public String getDynamic()
    {
        return dynamic;
    }

    /**
     * Returns the URI associated with nodes expansion.
     * @return URI
     */
    public String getExpandUrl()
    {
        return expandUrl;
    }

    /**
     * Returns the icon associated with top level node.
     * @return icon URI
     */
    public String getIcon()
    {
        return icon;
    }

    /**
     * Returns the label associated with top level node.
     * @return label
     */
    public String getLabel()
    {
        return label;
    }

    /**
     * Returns the list of levels which define the view.
     * @return List of JBLevel
     */
    public List getLevelList()
    {
        return levelList;
    }

    /**
     * Returns the view name.
     * @return name
     */
    public String getName()
    {
        return name;
    }

    /**
     * Returns the view plugin.
     * @return plugin class name
     */
    public String getPlugin()
    {
        return plugin;
    }

    /**
     * Sets the dynamic flag.
     * @param adynamic The dynamic to set ("true" or "false")
     */
    public void setDynamic(String adynamic)
    {
        this.dynamic = adynamic;
    }

    /**
     * Sets the URI associated with nodes expansion.
     * @param aexpandUrl The URI to set
     */
    public void setExpandUrl(String aexpandUrl)
    {
        this.expandUrl = aexpandUrl;
    }

    /**
     * Sets the icon associated with top level node.
     * @param aicon The icon URI to set
     */
    public void setIcon(String aicon)
    {
        this.icon = aicon;
    }

    /**
     * Sets the label associated with top level node.
     * @param alabel The label to set
     */
    public void setLabel(String alabel)
    {
        this.label = alabel;
    }

    /**
     * Adds a level which define the view.
     *
     * @param alevel level description to add
     */
    public void addLevel(JBLevel alevel)
    {
        this.levelList.add(alevel);
    }

    /**
     * Sets the view name.
     * @param aname The name to set
     */
    public void setName(String aname)
    {
        this.name = aname;
    }

    /**
     * Adds an objectview in the view.
     *
     * @param ov objectview to add
     */
    public void addObjectView(JBObjectView ov)
    {
        this.objectviewMap.put(ov.getName().trim().toLowerCase(), ov);
    }

    /**
     * Gets an objectview from it name.
     *
     * @param aname objectview name
     *
     * @return objectview or null if not found
     */
    public JBObjectView findObjectViewByName(String aname)
    {
        // rendre case insensitive en mettant le param en lowercase
        String key = aname.trim().toLowerCase();

        return (JBObjectView) objectviewMap.get(key);
    }

    /**
     * Adds a view specific parameter.
     *
     * @param key parameter name
     * @param value parameter value
     */
    public void addParam(String key, String value)
    {
        param.setProperty(key, value);
    }

    /**
     * Sets the view plugin.
     * @param aplugin The plugin to set
     */
    public void setPlugin(String aplugin)
    {
        this.plugin = aplugin;
    }

    /**
     * Displays the view definition.
     *
     * @return String formatted view definition
     */
    @Override
	public String toString()
    {
        StringBuffer buf = new StringBuffer();
        java.util.Iterator it = null;

        buf.append("   name = ");
        buf.append(name);
        buf.append(System.getProperty("line.separator"));
        buf.append("   dynamic = ");
        buf.append(dynamic);
        buf.append(System.getProperty("line.separator"));
        buf.append("   plugin = ");
        buf.append(plugin);
        buf.append(System.getProperty("line.separator"));
        buf.append("   expandUrl = ");
        buf.append(expandUrl);
        buf.append(System.getProperty("line.separator"));
        buf.append("   label = ");
        buf.append(label);
        buf.append(System.getProperty("line.separator"));
        buf.append("   icon = ");
        buf.append(icon);
        buf.append(System.getProperty("line.separator"));

        if (param != null)
        {
            buf.append("   Properties components of param = ");
            buf.append(System.getProperty("line.separator"));

            Enumeration enum1 = param.propertyNames();
            String key;

            while (enum1.hasMoreElements())
            {
                buf.append("   ...");
                key = (String) enum1.nextElement();
                buf.append(key);
                buf.append(" = ");
                buf.append(param.getProperty(key));
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (objectviewMap != null)
        {
            buf.append("   Map components of objectviewMap = ");
            buf.append(System.getProperty("line.separator"));
            it = objectviewMap.values().iterator();

            while (it.hasNext())
            {
                buf.append("   ...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        if (levelList != null)
        {
            buf.append("   List components of levelList = ");
            buf.append(System.getProperty("line.separator"));
            it = levelList.iterator();

            while (it.hasNext())
            {
                buf.append("   ...");
                buf.append(it.next());
                buf.append(System.getProperty("line.separator"));
            }
        }

        return buf.toString();
    }

    // end of toString method

    /**
     * Returns the table of objectviews.
     * @return Table of objectviews.
     */
    public Map getObjectviewMap()
    {
        return objectviewMap;
    }

    /**
     * Returns the list of profiles which can see the view.
     * @return List of profiles which can see the view
     */
    public JBViewDefProfiles getJbViewDefProfiles()
    {
        return jbViewDefProfiles;
    }

    /**
     * Sets the t of profiles which can see the view.
     * @param ajbViewDefProfiles The list of profiles to set
     */
    public void setJbViewDefProfiles(JBViewDefProfiles ajbViewDefProfiles)
    {
        this.jbViewDefProfiles = ajbViewDefProfiles;
    }

    /**
     * Compares two views.<br>
     * Comparison is performed on view names (case insensitive comparison).
     *
     * @param o object to be compared with
     * @return see Comparable.compareTo
     */
    @Override
	public int compareTo(Object o)
    {
        if (o instanceof JBViewDef)
        {
            return getName().trim().toLowerCase().compareTo(((JBViewDef) o).getName()
                                                             .trim()
                                                             .toLowerCase());
        }
        else
        {
            return 0;
        }
    }

    /**
     * Gets the label URL.
     * @return Returns the label URL.
     */
    public String getLabelURL()
    {
        return labelURL;
    }

    /**
     * Sets the label URL.
     * @param alabelURL The label URL to set.
     */
    public void setLabelURL(String alabelURL)
    {
        this.labelURL = alabelURL;
    }
}
